package com.nidu.demo.company.model;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.base.BaseDeletableEntity;
import com.nidu.demo.base.BaseStatusEntity;
import com.nidu.demo.base.BaseTenantEntity;
import com.nidu.demo.common.enums.BooleanEnum;
import com.nidu.demo.common.enums.StatusEnum;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

/**
 * 公司领域对象
 *
 * @author auto
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class Company extends BaseTenantEntity implements BaseStatusEntity, BaseDeletableEntity {

    private Long id;

    /**
     * 公司名称
     */
    private String companyName;

    /**
     * 公司地址
     */
    private String companyAddress;

    /**
     * 公司联系电话
     */
    private String companyPhoneNumber;

    /**
     * 排序值
     */
    private Integer sortOrder;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 是否删除
     */
    private Boolean deleted;

    /**
     * 创建公司
     * @param name
     * @param address
     * @param phoneNumber
     * @param sortOrder
     * @return
     */
    public static Company create(String name, String address, String phoneNumber, Integer sortOrder) {
        Company company = new Company();
        company.setCompanyName(name)
                .setCompanyAddress(address)
                .setCompanyPhoneNumber(phoneNumber)
                .setSortOrder(sortOrder);
        return company;
    }

    /**
     * 更新公司
     * @param name
     * @param address
     * @param phoneNumber
     * @param sortOrder
     * @return
     */
    public static Company update(Long id, String name, String address, String phoneNumber, Integer sortOrder) {
        Company company = new Company();
        company.setId(id)
                .setCompanyName(name)
                .setCompanyAddress(address)
                .setCompanyPhoneNumber(phoneNumber)
                .setSortOrder(sortOrder);
        return company;
    }

    @Override
    public void isDeleted() {
        if (BooleanEnum.TRUE.getValue().equals(this.deleted)) {
            throw new BizException(ErrorCodeConstants.COMPANY_NOT_FOUND.getCode(), ErrorCodeConstants.COMPANY_NOT_FOUND.getMessage());
        }
    }

    @Override
    public void isDisable() {
        if (StatusEnum.DISABLE.getValue().equals(this.status)) {
            throw new BizException(ErrorCodeConstants.COMPANY_STATUS_DISABLE.getCode(), ErrorCodeConstants.COMPANY_STATUS_DISABLE.getMessage());
        }
    }
}